package com.wuzao.aop.aspect;

import com.wuzao.aop.annotation.UserAccess;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
@Order(1)
public class UserAccessAspect {

    @Pointcut(value = "@annotation(com.wuzao.aop.annotation.UserAccess)")
    public void access() {

    }

    @Before("access()")
    public void deBefore(JoinPoint joinPoint) throws Throwable {
        log.info("@Order(1) @Before @UserAccess");
    }

    @Around("@annotation(userAccess)")
    public Object around(ProceedingJoinPoint joinPoint, UserAccess userAccess) {


        Object result = null;
        //获取注解里的值
        log.info("@Order(1) @Around @UserAccess 执行前:" + userAccess.desc());
        for (int i = 0; i < joinPoint.getArgs().length; i++) {
            System.out.println(joinPoint.getArgs()[i]);
        }
        System.out.println(joinPoint.getSignature().getName());
        try {
            result = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            result = null;
        }
        log.info("@Order(1) @Around @UserAccess 执行后:" + userAccess.desc());
        return result;
    }
}